package android.slc.mp.ui.page;

import android.slc.mp.po.SelectEvent;
import android.slc.mp.po.i.IBaseFolder;
import android.slc.mp.po.i.IBaseItem;
import android.slc.mp.po.i.IBaseResult;
import android.slc.mp.ui.SlcIMpDelegate;

import androidx.annotation.MainThread;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by slc
 * @param <S>
 * @param <F>
 * @param <T>
 */
public abstract class SlcMpPagerBaseVirtualVm<S extends IBaseResult<F, T>, F extends IBaseFolder<T>, T extends IBaseItem>
        implements SlcMpPagerVirtualVm<S, F, T>,
        SlcIMpDelegate.OnSelectEventListener {
    private final int mMediaType;
    private final SlcIMpDelegate mMediaPickerDelegate;
    private S mResult;
    private List<T> mCurrentMediaList;
    //

    private WeakReference<OnResultListener<List<T>>> mOnDataRefreshListenerWr;

    public SlcMpPagerBaseVirtualVm(int mediaType, SlcIMpDelegate mediaPickerDelegate) {
        this.mMediaType = mediaType;
        this.mMediaPickerDelegate = mediaPickerDelegate;
        this.mMediaPickerDelegate.addOnSelectEventListener(this);
    }

    @Override
    public int getMediaType() {
        return mMediaType;
    }

    @Override
    public SlcIMpDelegate getMediaPickerDelegate() {
        return mMediaPickerDelegate;
    }

    /**
     * 响应结果
     *
     * @param result
     */
    @MainThread
    @Override
    public void onResult(S result) {
        this.mResult = result;
        notifyLoadCompatListener();
    }

    @MainThread
    @Override
    public S getResult() {
        if (mResult == null) {
            throw new IllegalStateException("数据未加载");
        }
        return mResult;
    }

    @MainThread
    @Override
    public List<T> getCurrentMediaList() {
        if (mCurrentMediaList == null) {
            mCurrentMediaList = new ArrayList<>();
            mCurrentMediaList.addAll(getResult().getAllItems());
        }
        return mCurrentMediaList;
    }

    /**
     * 通知完成监听
     */
    protected void notifyLoadCompatListener(){
        OnResultListener<List<T>> onLoadCompatListener = getOnDataRefreshListener();
        if (onLoadCompatListener != null) {
            onLoadCompatListener.onLoadResult(getCurrentMediaList());
        }
    }

    /////事件相关///////
    @Override
    public boolean selectItem(int position) {
        T item = mCurrentMediaList.get(position);
        //此处调用mMediaPickerDelegate尽力全局的选择
        return item.isChecked() ? mMediaPickerDelegate.removeItem(item) : mMediaPickerDelegate.addItem(item);
    }

    @Override
    public void setCurrentMediaList(List<T> mediaList) {
        this.mCurrentMediaList.clear();
        this.mCurrentMediaList.addAll(mediaList);
    }

    @Override
    public Object onSelectEvent(int eventCode, SelectEvent event) {
        switch (eventCode) {
            case SelectEvent.EVENT_LISTENER_MEDIA_TYPE:
                return getListenerMediaType();
        }
        return null;
    }

    @Override
    public int getListenerMediaType() {
        return getMediaType();
    }

    @Override
    public void setOnDataRefreshListener(OnResultListener<List<T>> onDataRefreshListener) {
        this.mOnDataRefreshListenerWr = new WeakReference<>(onDataRefreshListener);
    }

    protected OnResultListener<List<T>> getOnDataRefreshListener() {
        return this.mOnDataRefreshListenerWr.get();
    }

    @Override
    public void onVCleared() {

    }
}
